<?php
/*
 * 手机客户端展品列表接口
 * 2013-11-20
 */
include "inc/public_header.php";


$reJson = array(); //构建返回值

/*
 * 常量
 */

$img_Path = "http://www.chinalaohu.com/img1";
$img_basePath = "http://www.chinalaohu.com/modules/miniature.php?w=180&h=180&p=/uploads/1000*1000/";
$img_zjl_basePath = "http://www.chinalaohu.com/modules/miniature.php?w=180&h=180&p=/uploads/zjl/";

$platform		= $_REQUEST["platform"];		//platform  0安卓 1苹果
$groupFilter 	= $_REQUEST["groupFilter"];		//过滤父类别，如：品牌分类, 类别分类, 规格
$childFilter 	= $_REQUEST["childFilter"];		//过滤子类别，如：钲艺廊, 稻房
$page			= $_REQUEST["page"]; 			//页数，第一页为0
$pagesize		= $_REQUEST["pagesize"];		//每页记录数

$title			= $_REQUEST["title"];			//标题过滤
$uuid			= $_REQUEST["uuid"];			//会员主键
$asid			= $_REQUEST["asid"];			//专场主键
$showSF			= $_REQUEST["showSF"];			//是否返回过滤,排序信息,  1:返回; 0:不返回

$groupFilterArr = explode(",",$groupFilter);	//转换为数组  sort
$childFilterArr = explode(",",$childFilter);	//转换为数组

if(count($groupFilterArr)!=count($childFilterArr)){
	echo json_encode(array("success"=>"0","msg"=>"groupFilter与childFilter数量不等"));
	die;
}

if(!isset($page)){
	$page = 0;
}
if(!isset($pagesize)){
	$pagesize = 15; //默认每页15条
}


//获取符合条件的展品id
$sql = "select aid from auction where is_delete=0 and atag>0  ";
//标题名过滤
if($title){
	$sql = $sql." and auction.auction like '%".$title."%'";
}
//专场过滤
if($asid){
	$sql = $sql." and auction.asid='".$asid."'";
}
$sortValue = ""; //排序类型值
if(count($groupFilterArr)>0){
	for($i=0;$i<count($groupFilterArr);$i++){
		if(!$groupFilterArr[$i] || !$childFilterArr[$i]){
			continue;
		}
		if($groupFilterArr[$i]=="sprice"){
			$priceArr = explode("|",$childFilterArr[$i]);
			$sql = $sql." and if(lastprice=0,startprice,lastprice)>=".$priceArr[0]." and if(lastprice=0,startprice,lastprice)<".$priceArr[1];
		}else if($groupFilterArr[$i]=="sort"){
			//获取排序标识
			$sortValue = $childFilterArr[$i];
		}else{
			$sql = $sql." and ".$groupFilterArr[$i]."='".$childFilterArr[$i]."'";
		}
	}
}
//处理排序
$orderBy = "";
switch ($sortValue){
	case "2":
		//价格由高到低
		$orderBy = "b.asprevtime desc,if(lastprice = 0,startprice,lastprice) desc";
		break;
	case "3":
		//价格由低到高
		$orderBy = "b.asprevtime desc,if(lastprice = 0,startprice,lastprice) asc";
		break;
	case "4":
		//人气
		$orderBy = "b.asprevtime desc,bids desc";
		break;
	default:
		$orderBy = "b.asprevtime desc, acode asc";
		break;
}

//查询并获取aid数组
$aidArr = $db->queryArray($sql);
$listCount = count($aidArr); //记录数
$aids = "";
for($i=0;$i<$listCount;$i++){
	if($i>0){
		$aids = $aids.",";
	}
	$aids = $aids.$aidArr[$i][0];
}
if(!$aids){
	$aids = "0";
}
$sql = "
	select
		a.aid,
		a.atype,
		a.atag,
		a.acode,
		a.auction as aname,
		a.startprice,
		a.oneprice,
		a.lastprice,
		if(a.lastprice=0,a.startprice,a.lastprice) as showprice,
		b.asprevtime,
		b.asprevclosetime,
		b.asopentime,
		b.asclosetime,
		b.astid,
		a.leixin,
		a.zp_img_name,
		c.img_waterfall,
		if(b.asclosetime<=now(),2,if(b.asopentime<=now(),1,0)) as auction_flag
	from
		auction a
		inner join auction_special b on a.asid=b.asid
		inner join (select DISTINCT(aid),img_waterfall from auction_gallery) c on a.aid=c.aid
	where 
		 a.aid in (".$aids.")
";
if($orderBy){
	$sql = $sql." order by ".$orderBy.",aid desc";
}
$sql = $sql." limit ".($page * $pagesize).",".$pagesize;

//查询指定展品
$auctionArr = $db->queryArray($sql);

$userAidArr = array();
if($uuid && strlen($uuid)>=32){
	$sql = "
		select aid
		from user_concern
		inner join user_base on user_base.uid = user_concern.uid
		where user_base.muuid = '".$uuid."'
	";
	$db->query($sql);
	while($db->next_record()){
		$userAidArr[] = $db->f("aid");
	}
}
$reJson["success"] 	= "1";
//echo json_encode($userAidArr);

if($showSF && intval($showSF,10)==1){
	$filter 		= array();
	$classchild 	= array();
	$brandchild	= array();
	
	/*  分类  */
	if($title && strlen($title)>0){
		$sql = "select pcid,pclass from pin_class where fpcid = 0 order by pcorder asc";
		$db->query($sql);
		while($db->next_record()){
			$classchild[] = array(
				"name"=>$db->f("pclass")
				,"value"=>$db->f("pcid")
			);
		}
		if(count($classchild)>0){
			$filter[] = array(
							"name"=>"分类"
							,"value"=>"pcid"
							,"child"=>$classchild
						);
		}
	}
	
	
	/* 材质 */
	$sql = "select distinct auction.chizhi from auction where chizhi <> '' and aid in (".$aids.")";
	$db->query($sql);
	$chizhichild = array();
	while($db->next_record()){
		$chizhichild[] = array(
			"name"=>$db->f("chizhi")
			,"value"=>$db->f("chizhi")
		);
	}
	if(count($chizhichild)>0){
		$filter[] = array(
			"name"=>"材质"
			,"value"=>"chizhi"
			,"child"=>$chizhichild
		);
	}
	
	/* 类型 */
	$sql = "select distinct auction.leixin from auction where leixin <> '' and aid in (".$aids.") ";
	$db->query($sql);
	$leixinchild = array();
	while($db->next_record()){
		$leixinchild[] = array(
			"name"=>$db->f("leixin")
			,"value"=>$db->f("leixin")
		);
	}
	if(count($leixinchild)>0){
		$filter[] = array(
			"name"=>"类型"
			,"value"=>"leixin"
			,"child"=>$leixinchild
		);
	}
	
	/* 价格 */
	$filter[]  = array(
		 "name"=>"价格区间"
		,"value"=>"sprice"
		,"child"=>array(
			 array("name"=>"0-50元","value"=>"0|50")
			,array("name"=>"50-100元","value"=>"50|100")
			,array("name"=>"100-500元","value"=>"100|500")
			,array("name"=>"500-1000元","value"=>"500|1000")
			,array("name"=>"1000-2000元","value"=>"1000|2000")
			,array("name"=>"2000-5000元","value"=>"2000|5000")
			,array("name"=>"5000元以上","value"=>"5000|999999999")
		)
	);
	
	/* 排序 */
	$filter[]  = array(
		 "name"=>"排序"
		,"value"=>"sort"
		,"child"=>array(
			 array("name"=>"默认","value"=>"0")
			,array("name"=>"价格从低到高","value"=>"3")
			,array("name"=>"价格从高到低","value"=>"2")
			,array("name"=>"人气排序","value"=>"4")
		)
	);
	
	/* 放入过滤器 */
	$reJson["filter"] = $filter;
}

$reJson["count"] = $listCount;
$reJson["serverTime"] = date('Y-m-d H:i:s');

//循环处理展品记录
$list = array();
for($i=0;$i<count($auctionArr);$i++){

	//是否标记为关注
	if(in_array($auctionArr[$i]["aid"],$userAidArr)){
		$attention = 1;
	}else{
		$attention = 0;
	}
	$listjson = array(
		 "aID"=>$auctionArr[$i]["aid"]
		,"iconUrl"=>$img_Path.str_replace("/upload/", "/upload/ios/", $auctionArr[$i]["img_waterfall"])
		,"auctionCode"=>$auctionArr[$i]["acode"]
		,"name"=>$auctionArr[$i]["aname"]
		,"price"=>$auctionArr[$i]["showprice"]
		,"attention"=>strval($attention)
		,"leixin"=>(string)$auctionArr[$i]["leixin"]
		,"preSTime"=>$auctionArr[$i]["asprevtime"]
		,"preETime"=>$auctionArr[$i]["asprevclosetime"]
		,"lastTime"=>$auctionArr[$i]["asclosetime"]
		,"dealTime"=>$auctionArr[$i]["asclosetime"]
		,"auctionType"=>$auctionArr[$i]["auction_flag"]
	);
	
	$list[] = $listjson;
}
$reJson["list"] = $list;
echo json_encode($reJson);
die;
	
/**
 * 判断字符串开头是否为指定字符
 * $str 待判断字符串
 * $part 指定字符
 */
function startsWith($str,$part){
	//若第一个字符串长度小于第二个的时候，必须指定返回false，
	//否则substr_compare遇到这种情况会返回0（即相当，与事实不符合）
	if(strlen($str)<strlen($str)){
		return false;
	}else{
		////当第一个长度大于第二个时才开始判断
		return !substr_compare($str,$part,0,strlen($part));
	}
}
?>
